package cn.rui.service.impl;

import cn.rui.entity.PageResult;
import cn.rui.entity.QueryPageBean;
import cn.rui.mapper.MenuMapper;
import cn.rui.mapper.PermissionMapper;
import cn.rui.mapper.RoleMapper;
import cn.rui.mapper.UserMapper;
import cn.rui.pojo.Menu;
import cn.rui.pojo.Permission;
import cn.rui.pojo.Role;
import cn.rui.pojo.User;
import cn.rui.service.UserService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.*;

/**
 * @author 徽州大都督
 * @date 2020/8/30
 */
@Transactional
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private PermissionMapper permissionMapper;
    @Autowired
    private MenuMapper menuMapper;

    /**
     * 根据用户名查询用户
     * @param username
     * @return
     */
    public User findByUsername(String username) {
        User user = userMapper.findByUsername (username);

        //根据用户id查询角色信息role
        Set<Role> roleList = roleMapper.findByUserId (user.getId ());
        user.setRoles (roleList);

        for (Role role : roleList) {
            //根据角色id查询关联的权限信息
            Set<Permission> permissions = permissionMapper.findByRoleId (role.getId ());
            role.setPermissions (permissions);
        }
        return user;
    }

    /**
     * 获取当前登陆用户可以显示的菜单
     * @param user
     * @return
     */
    public List<Menu> findMenuByUsername(User user) {
        //根据用户id查询角色信息role
        Set<Role> roles = roleMapper.findByUsersId (user.getId ());

        //根据角色id查询菜单信息
        Set<Menu> menuSet =new HashSet<> ();
        for (Role role : roles) {
            menuSet=menuMapper.findByRoleId(role.getId ());
        }

        List<Menu> menus= new ArrayList<> ();
        List<Menu> menus1 = new ArrayList<> ();
        for (Menu menu : menuSet) {
            if (menu.getLinkUrl ()==null){
                menus.add (menu);  //主菜单
            }else {
                menus1.add (menu);  //子菜单
            }
        }

        for (Menu menu : menus) {
            List<Menu> list = new ArrayList<> ();
            for (Menu menu1 : menus1) {
                if (menu.getId ()==menu1.getParentMenuId ()){
                    list.add (menu1);
                }
            }

            //子菜单排序
            Collections.sort (list, new Comparator<Menu> () {
                @Override
                public int compare(Menu o1, Menu o2) {
                    int i =o1.getPriority ()-o2.getPriority ();

                    return i;
                }
            });

            menu.setChildren (list);
        }


       /* for (Menu menu : menus) {
                List<Menu> menuList = menuMapper.findByParentMenuId(menu.getId ());
                menu.setChildren (menuList);
        }*/


        /*for (Menu menu : menuSet) {
            if (menu.getLinkUrl ()!=null){
                menuSet.remove (menu);
            }
        }*/

        //排序
        Collections.sort (menus, new Comparator<Menu> () {
            @Override
            public int compare(Menu o1, Menu o2) {
                int i =o1.getPriority ()-o2.getPriority ();

                return i;
            }
        });

        return menus;
    }

    /**
     * 分页查询用户
     * @param queryPageBean
     * @return
     */
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage ();
        Integer pageSize = queryPageBean.getPageSize ();
        String queryString = queryPageBean.getQueryString ();
        PageHelper.startPage (currentPage,pageSize);
        Page<User> page = userMapper.findPage(queryString);
        long total = page.getTotal ();
        List<User> result = page.getResult ();

        for (User user : result) {
            Set<String> roleName= userMapper.findRoleByUserId(user.getId ());
            String string = roleName.toString ();
            user.setRoleName (string);
        }

        return new PageResult (total,result);
    }

    /**
     * 编辑信息回显
     * @return
     */
    public Map findById(Integer id) {
        Map<String,Object> map = new HashMap<> ();

        User user=userMapper.findById(id);

        Set<Role> roles = roleMapper.findByUsersId (id);
        String roleName="";
        for (Role role : roles) {
            roleName+=role.getName ();
        }
        map.put ("username",user.getUsername ());
        map.put ("password",user.getPassword ());
        map.put ("telephone",user.getTelephone ());
        map.put ("formDataRoleid",roleName);
        map.put ("status",String.valueOf (user.getStation ()));
        map.put ("remark",user.getRemark ());
        map.put ("id",user.getId ());
        return map;
    }

    /**
     * 编辑用户信息
     * @param roleName
     * @param map
     */
    public void edit(String roleName, Map map) {
        User user =new User ();
        user.setUsername ((String) map.get ("username"));
        user.setPassword ((String) map.get ("password"));
        user.setTelephone ((String) map.get ("telephone"));
        user.setStation (Integer.parseInt (map.get ("status").toString ()));
        user.setId (Integer.parseInt (map.get ("id").toString ()));
        user.setRemark ((String) map.get ("remark"));
        //修改用户信息
        userMapper.update(user);

        //查询用户拥有的角色
        Role role=roleMapper.findByName(roleName);

        Map<String,Object> map1 = new HashMap<> ();
        map1.put ("user_id",user.getId ());
        map1.put ("role_id",role.getId ());
        roleMapper.update(map1);
    }

    /**
     * 修改登陆密码
     * @param username
     * @param encodePassword
     */
    public void updatePassword(String username, String encodePassword) {

        //根据用户名查询用户
        User user = userMapper.findByUsername (username);
        user.setPassword (encodePassword);

        //修改
        userMapper.update (user);
    }
}
